package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.entity.User;
import org.example.service.OrderService;
import org.example.service.UserService;
import org.example.utils.Constants;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.Type;


/**
 * @author 张在成
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @RequestMapping(value = "/order/{orderId}")
    public Result getOrderInfoWithUser(@PathVariable("orderId") Integer orderId, String flag){
        StringBuffer sb=null;
        ServiceInstance serviceInstance=loadBalancerClient.choose("order-service");

        if (serviceInstance==null){
            return  null;
        }
        sb=new StringBuffer();
        sb.append("http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/order/"+orderId);
        System.out.println(sb.toString());

        //Result result = restTemplate.getForObject(sb.toString(), Result.class);
        ResponseEntity<Result> responseEntity=restTemplate.exchange(sb.toString(), HttpMethod.GET, null, new ParameterizedTypeReference<Result>() {});

        //log.info("根据订单id查询订单信息："+orderId);
        //Result result=restTemplate.getForObject("http://order-service/order/"+orderId, Result.class);

        return  responseEntity.getBody();
    }

    @RequestMapping(value = "/info/{orderId}")
    public Result getUserInfoWithUsername(@PathVariable("orderId") Integer orderId) throws Exception {
        log.info("根据orderId查询用户信息："+orderId);
        Result result=orderService.selectOrderInfoById(orderId);
        return  result;
    }





}
